﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BLL;


namespace Logic.BusinessLogic
{
	public class AccountService
	{
		public static List<BO.MasterAccount> GetMasterAccountsForProfile(int profileID, bool withAccounts = false)
		{
			var q = MasterAccounts.Get()
										.Where(MasterAccounts.Columns.ProfileID, Actions.Equal, profileID);
			if (withAccounts)
				q.Join(Accounts.Get().Where(Accounts.Columns.StatusEnum, Actions.Equal, EntityStatuses.Active)
						, Accounts.Columns.MasterAccountID);

			return q.Select();
		}

		public static List<BO.Account> GetAccountsByMasterID(int masterID)
		{
			var res = BLL.Accounts.Get().Where(Accounts.Columns.MasterAccountID, Actions.Equal, masterID).Select();
			return res;
		}

		public static void UpdateFbToken(int accountID, string accessToken)
		{
			var account = BLL.Accounts.GetByID(accountID);
			account.FbToken = accessToken;
			account.Update();
		}

		public static void UpdateTwToken(int accountID, string accessToken, string AccessTokenSecret, long uid, string screenName)
		{
			var account = BLL.Accounts.GetByID(accountID);
			account.TwToken = accessToken;
			account.TwSecret = AccessTokenSecret;
			account.TwUserID = uid;
			//account.TwUsername = screenName;
			account.Update();
		}

		public static void UpdateYtToken(int accountID, string accessToken)
		{
			var account = BLL.Accounts.GetByID(accountID);
			account.YtToken = accessToken;
			account.Update();
		}

		public static bool IsUserAutorisedForAccount(BO.User User, BO.MasterAccount mAccount)
		{
			return mAccount != null && User != null && mAccount.ProfileID == User.ProfileID;
		}
	}
}
